package com.fare.taluo.core.domain.crawl.adapter;

import java.util.Date;

import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.lang3.time.DateUtils;

import com.fare.taluo.core.domain.ChannelConst;
import com.fare.taluo.core.domain.crawl.CrawlTaskModel;
import com.fare.taluo.core.domain.crawl.adapter.sub.AirchinaAdapter;
import com.fare.taluo.core.domain.crawl.adapter.sub.CSAirAdaptor;
import com.fare.taluo.core.domain.crawl.adapter.sub.HaiNanAirHttpClientAdapter;
import com.fare.taluo.core.domain.crawl.adapter.sub.MUAirAdapter;
import com.fare.taluo.core.domain.crawl.adapter.sub.ShenzhengAirHttpClientAdapter;
import com.fare.taluo.core.domain.entity.crawl.FreightPriceEntity;
import com.fare.taluo.util.StringUtils;
import com.google.protobuf.TextFormat.ParseException;

/**
 * 抓取适配器简单工厂
 * @author xiangsf 
 * @since 2013-1-31
 */
public class CrawlAdapterFactory {
	
   /**
    * 根据获取的渠道标识获得适配器实例
    * <p>目前无法从queue对象上获得渠道标识, 以后采用类反射
    * @param channelId
    * @param queue
    * @return
    */
   public static AbstractAdapter buildAdaptor(CrawlTaskModel task){
	   AbstractAdapter adapter = null;
	   long channelId = task.getGrabChannelId();
	   
	   //下载适配器
//	   public void FindAdapterClass() throws IOException{
//		   
//		   try{
//			   
//		   }catch (Exception e) {
//			e.printStackTrace();
//		}
//		   
//	   }
	   //实例化适配器
	   
	   //TODO:根据任务ID，到适配器配置管理模块，获取最新可用的适配器回来，并实例化
	   //下载适配器，存放filepath,  classNames[]
	   //根据已下载文件及类名，创建适配器实例，HotswapCL k=new HotswapCL(filepath,classNames)
	   //并加入到map<channelId>,<类实例>中
//	   String filepath="";
//	   String[] classNames = null;
//	   HotswapCL k=new HotswapCL(filepath,classNames);
//	   try {
//		k.loadClass(classNames[0]);
//	} catch (ClassNotFoundException e) {
//		// TODO Auto-generated catch block
//		e.printStackTrace();
//	}
//	   
	   
	   
	   
//	   switch(channelId){
//	   case ChannelConst.CHANNEL_AIRCHINA:adapter = new AirchinaAdapter(task);
//	   case 
//	   }
	   
//	   map<channelId>
	   
	   if(ChannelConst.CHANNEL_AIRCHINA == channelId){
		   adapter = new AirchinaAdapter(task);
	   }else if(ChannelConst.CHANNEL_CS == channelId){
		   adapter = new CSAirAdaptor(task);
	   }else if(ChannelConst.CHANNEL_HN == channelId){
		   adapter = new HaiNanAirHttpClientAdapter(task);
	   }else if(ChannelConst.CHANNEL_MU == channelId){
		   adapter = new MUAirAdapter(task);
	   }else if(ChannelConst.CHANNEL_SZ == channelId){
		   adapter = new ShenzhengAirHttpClientAdapter(task);
	   }
	   
	  return adapter;
	  
   }
	
	/**
	 * 得到起飞/gcj dp 时间
	 * <p>由flightBatchDate+flightBatchStartTime组合而成
	 * @return
	 * @throws ParseException 
	 * @throws java.text.ParseException 
	 */
	public static Date getFlightTime(String flightBatchDate, String timez) {
		if(StringUtils.isNotEmpty(flightBatchDate) && StringUtils.isNotEmpty(timez)){
			try{
				String time = timez.indexOf(":") > -1 ? timez.replace(":", "") : timez;
				if(time.length() == 3)  time = "0"+time;
				else if(time.length() == 2) time = "00"+time;
				else if(time.length() == 1) time = "000"+time;
				return DateUtils.parseDate(flightBatchDate+" "+time, "yyyy-MM-dd HHmm");
			}catch(java.text.ParseException e){
				//logger.error(e);
			}
		}
		return null;
	}
	/**
	 * 创建PlaneInfo对象
	 * @param taskEntity 任务对象
	 * @param carrierKey
	 * @param carrierName
	 * @param carrierFullName
	 * @param startTime
	 * @param endTime
	 * @param flightNo
	 * @param lowerPrice
	 * @param agentName
	 * @param highPrice
	 * @param type
	 * @return
	 */
	public static FreightPriceEntity createPlaneInfo(CrawlTaskModel taskEntity,
			String carrierKey, String carrierName,
			String carrierFullName,  String startTime,
			String endTime, String flightNo, String lowerPrice,
			String agentName, String highPrice, String type){
		FreightPriceEntity entity = new FreightPriceEntity(taskEntity);
		entity.setCarrierKey(StringUtils.trimEmpty(carrierKey));
		entity.setCarrierName(StringUtils.trimEmpty(carrierName));
		entity.setCarrierFullName(StringUtils.trimEmpty(carrierFullName));
		
		entity.setEndTime(CrawlAdapterFactory.getFlightTime(entity.getFlightBatchDate(), endTime));
		entity.setStartTime(CrawlAdapterFactory.getFlightTime(entity.getFlightBatchDate(), startTime));
		//因为页面获得的数据有时会出现跨天的情况，所以这里加了一个判断，如果结束日期在开始日期之前，则将结束时间加一天
		if(entity.getStartTime()!=null &&
				entity.getEndTime()!=null && entity.getEndTime().before(entity.getStartTime())){
			entity.setEndTime(DateUtils.addDays(entity.getEndTime(), 1));
		}
		entity.setFlightId(StringUtils.trimEmpty(flightNo));
		entity.setHighPrice(StringUtils.isNotBlank(highPrice) && NumberUtils.isNumber(highPrice) ? Double.valueOf(highPrice.trim()) : null);
		entity.setLowerPrice(StringUtils.isNotBlank(lowerPrice) && NumberUtils.isNumber(lowerPrice) ? Double.valueOf(lowerPrice.trim()) : null);
		entity.setAgentName(StringUtils.trimEmpty(agentName));
		entity.setFlightType(type);
		return entity;
	}
	
	
	
	
	
	
	
}
 

